﻿'*******************************************************************************
'*******************************************************************************
'** Copyright 2008 Robert Burke                                              ***
'** r.burke.itt@gmail.com                                                    ***
'**                                                                          ***
'** Licensed under the Apache License, Version 2.0 (the "License");          ***
'** you may not use this file except in compliance with the License.         ***
'** You may obtain a copy of the License at:                                 ***
'**                                                                          ***
'** http://www.apache.org/licenses/LICENSE-2.0                               ***
'**                                                                          ***
'** Unless required by applicable law or agreed to in writing, software      ***
'** distributed under the License is distributed on an "AS IS" BASIS,        ***
'** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ***
'** See the License for the specific language governing permissions and      ***
'** limitations under the License.                                           ***
'*******************************************************************************
'*******************************************************************************

Public Class frmSysTray
    Private Sub NotePad_State()
        TrayPad.Visible = Not TrayPad.Visible
        TrayPad.Activate()
    End Sub

    Public Sub FolderPad_State()
        FolderPad.Visible = Not FolderPad.Visible
        FolderPad.Activate()
    End Sub

    Private Sub NotifyIcon1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            NotePad_State()
        ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
            FolderPad_State()
        End If
    End Sub

    Private Sub frmSysTray_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FolderPad.Visible = False

        If My.Settings.traypad_start = 1 Then
            NotePad_State()
        End If

        Me.Hide()

        If Not System.IO.Directory.Exists(savePath) Then
            System.IO.Directory.CreateDirectory(savePath)
        End If

        If System.IO.File.Exists("C:\pad1.txt") And Not System.IO.File.Exists(savePath & "pad5.txt") Then
            System.IO.File.Copy("C:\pad1.txt", savePath & "pad1.txt")
        End If
        If System.IO.File.Exists("C:\pad2.txt") And Not System.IO.File.Exists(savePath & "pad5.txt") Then
            System.IO.File.Copy("C:\pad2.txt", savePath & "pad2.txt")
        End If
        If System.IO.File.Exists("C:\pad3.txt") And Not System.IO.File.Exists(savePath & "pad5.txt") Then
            System.IO.File.Copy("C:\pad3.txt", savePath & "pad3.txt")
        End If
        If System.IO.File.Exists("C:\pad4.txt") And Not System.IO.File.Exists(savePath & "pad5.txt") Then
            System.IO.File.Copy("C:\pad4.txt", savePath & "pad4.txt")
        End If
        If System.IO.File.Exists("C:\pad5.txt") And Not System.IO.File.Exists(savePath & "pad5.txt") Then
            System.IO.File.Copy("C:\pad5.txt", savePath & "pad5.txt")
        End If

        If System.IO.File.Exists("C:\padfolder.txt") And Not System.IO.File.Exists(savePath & "padfolder.txt") Then
            System.IO.File.Copy("C:\padfolder.txt", savePath & "padfolder.txt")
        End If

        Try
            If System.IO.File.Exists("C:\pad1.txt") And System.IO.File.Exists(savePath & "pad5.txt") Then
                System.IO.File.Delete("C:\pad1.txt")
            End If
            If System.IO.File.Exists("C:\pad2.txt") And System.IO.File.Exists(savePath & "pad5.txt") Then
                System.IO.File.Delete("C:\pad2.txt")
            End If
            If System.IO.File.Exists("C:\pad3.txt") And System.IO.File.Exists(savePath & "pad5.txt") Then
                System.IO.File.Delete("C:\pad3.txt")
            End If
            If System.IO.File.Exists("C:\pad4.txt") And System.IO.File.Exists(savePath & "pad5.txt") Then
                System.IO.File.Delete("C:\pad4.txt")
            End If
            If System.IO.File.Exists("C:\pad5.txt") And System.IO.File.Exists(savePath & "pad5.txt") Then
                System.IO.File.Delete("C:\pad5.txt")
            End If

            If System.IO.File.Exists("C:\padfolder.txt") And System.IO.File.Exists(savePath & "padfolder.txt") Then
                System.IO.File.Delete("C:\padfolder.txt")
            End If
        Catch ex As System.UnauthorizedAccessException
            MessageBox.Show("It seems you may have UAC enabled." & vbNewLine & "In an attempt to comply with Windows Security standards, SysPad data files for this user have been moved to your AppData folder." & vbNewLine & "This program needs to restart with UAC elvation once to complete this process." & vbNewLine & "Administrator privileges will not be required again in the future", "UAC detected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            Dim startInfo = New ProcessStartInfo()
            Dim p As Process

            startInfo.UseShellExecute = True
            startInfo.WorkingDirectory = Environment.CurrentDirectory
            startInfo.FileName = Application.ExecutablePath
            startInfo.Verb = "runas"

            p = Process.Start(startInfo)
            Me.Close()
        End Try
    End Sub
End Class